package com.app.shop.fragments;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.shop.R;
import com.app.shop.activitys.GoodsDetailActivity;
import com.app.shop.adapters.GoodsListAdapter;
import com.app.library.beans.EventMessage;

import com.app.shop.beans.GoodsBean;
import com.app.library.base.BaseFragment;
import com.chad.library.adapter.base.BaseQuickAdapter;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class FenleiFragment extends BaseFragment {


  private   RecyclerView recy;
    private TextView tv_0;
    private TextView tv_1;
    private TextView tv_2;
    private TextView tv_3;
    private  TextView tv_4;
    private  TextView tv_5;
    private TextView tv_6;
    private TextView tv_7;

    private List<GoodsBean> list = new ArrayList<>();
    private GoodsListAdapter adapter;

    int position = 0;

    public static FenleiFragment getInstance(){
        FenleiFragment fragment=new FenleiFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_fenlei, container, false);
       recy = view.findViewById(R.id.recy);
        tv_0 = view.findViewById(R.id.tv_0);
        tv_1 = view.findViewById(R.id.tv_1);
        tv_2 = view.findViewById(R.id.tv_2);
        tv_3 = view.findViewById(R.id.tv_3);
        tv_4 = view.findViewById(R.id.tv_4);
        tv_5 = view.findViewById(R.id.tv_5);
        tv_6 = view.findViewById(R.id.tv_6);
        tv_7 = view.findViewById(R.id.tv_7);

        registerEventBus();

        initList();
        return view;
    }

    //列表
    private void initList() {


        adapter = new GoodsListAdapter(R.layout.item_goods_list_gv, list);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.view_list_empty, null);
        TextView tv_empty = view.findViewById(R.id.tv_empty_title);
        tv_empty.setText("暂无商品，等待用户发布");
        adapter.setEmptyView(view);
        recy.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        recy.setAdapter(adapter);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("bean", list.get(position));
                skipActivity(getActivity(), GoodsDetailActivity.class, bundle);
            }
        });

        setSelPosit(position);
        tv_0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelPosit(0);
            }
        });

        tv_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelPosit(1);
            }
        });

        tv_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelPosit(2);
            }
        });

        tv_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelPosit(3);
            }
        });
        tv_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelPosit(4);
            }
        });
        tv_5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelPosit(5);
            }
        });
        tv_6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelPosit(6);
            }
        });
        tv_7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelPosit(7);
            }
        });

    }


    private void setSelPosit(int pos) {
        position = pos;
        switch (pos) {
            case 0:
                tv_0.setTextColor(getResources().getColor(R.color.red));
                tv_1.setTextColor(getResources().getColor(R.color.color_black));
                tv_2.setTextColor(getResources().getColor(R.color.color_black));
                tv_3.setTextColor(getResources().getColor(R.color.color_black));
                tv_4.setTextColor(getResources().getColor(R.color.color_black));
                tv_5.setTextColor(getResources().getColor(R.color.color_black));
                tv_6.setTextColor(getResources().getColor(R.color.color_black));
                tv_7.setTextColor(getResources().getColor(R.color.color_black));

                break;
            case 1:
                tv_0.setTextColor(getResources().getColor(R.color.color_black));
                tv_1.setTextColor(getResources().getColor(R.color.red));
                tv_2.setTextColor(getResources().getColor(R.color.color_black));
                tv_3.setTextColor(getResources().getColor(R.color.color_black));
                tv_4.setTextColor(getResources().getColor(R.color.color_black));
                tv_5.setTextColor(getResources().getColor(R.color.color_black));
                tv_6.setTextColor(getResources().getColor(R.color.color_black));
                tv_7.setTextColor(getResources().getColor(R.color.color_black));
                break;
            case 2:
                tv_0.setTextColor(getResources().getColor(R.color.color_black));
                tv_1.setTextColor(getResources().getColor(R.color.color_black));
                tv_2.setTextColor(getResources().getColor(R.color.red));
                tv_3.setTextColor(getResources().getColor(R.color.color_black));
                tv_4.setTextColor(getResources().getColor(R.color.color_black));
                tv_5.setTextColor(getResources().getColor(R.color.color_black));
                tv_6.setTextColor(getResources().getColor(R.color.color_black));
                tv_7.setTextColor(getResources().getColor(R.color.color_black));
                break;
            case 3:
                tv_0.setTextColor(getResources().getColor(R.color.color_black));
                tv_1.setTextColor(getResources().getColor(R.color.color_black));
                tv_2.setTextColor(getResources().getColor(R.color.color_black));
                tv_3.setTextColor(getResources().getColor(R.color.red));
                tv_4.setTextColor(getResources().getColor(R.color.color_black));
                tv_5.setTextColor(getResources().getColor(R.color.color_black));
                tv_6.setTextColor(getResources().getColor(R.color.color_black));
                tv_7.setTextColor(getResources().getColor(R.color.color_black));
                break;
            case 4:
                tv_0.setTextColor(getResources().getColor(R.color.color_black));
                tv_1.setTextColor(getResources().getColor(R.color.color_black));
                tv_2.setTextColor(getResources().getColor(R.color.color_black));
                tv_3.setTextColor(getResources().getColor(R.color.color_black));
                tv_4.setTextColor(getResources().getColor(R.color.red));
                tv_5.setTextColor(getResources().getColor(R.color.color_black));
                tv_6.setTextColor(getResources().getColor(R.color.color_black));
                tv_7.setTextColor(getResources().getColor(R.color.color_black));
                break;
            case 5:
                tv_0.setTextColor(getResources().getColor(R.color.color_black));
                tv_1.setTextColor(getResources().getColor(R.color.color_black));
                tv_2.setTextColor(getResources().getColor(R.color.color_black));
                tv_3.setTextColor(getResources().getColor(R.color.color_black));
                tv_4.setTextColor(getResources().getColor(R.color.color_black));
                tv_5.setTextColor(getResources().getColor(R.color.red));
                tv_6.setTextColor(getResources().getColor(R.color.color_black));
                tv_7.setTextColor(getResources().getColor(R.color.color_black));
                break;
            case 6:
                tv_0.setTextColor(getResources().getColor(R.color.color_black));
                tv_1.setTextColor(getResources().getColor(R.color.color_black));
                tv_2.setTextColor(getResources().getColor(R.color.color_black));
                tv_3.setTextColor(getResources().getColor(R.color.color_black));
                tv_4.setTextColor(getResources().getColor(R.color.color_black));
                tv_5.setTextColor(getResources().getColor(R.color.color_black));
                tv_6.setTextColor(getResources().getColor(R.color.red));
                tv_7.setTextColor(getResources().getColor(R.color.color_black));
                break;
            case 7:
                tv_0.setTextColor(getResources().getColor(R.color.color_black));
                tv_1.setTextColor(getResources().getColor(R.color.color_black));
                tv_2.setTextColor(getResources().getColor(R.color.color_black));
                tv_3.setTextColor(getResources().getColor(R.color.color_black));
                tv_4.setTextColor(getResources().getColor(R.color.color_black));
                tv_5.setTextColor(getResources().getColor(R.color.color_black));
                tv_6.setTextColor(getResources().getColor(R.color.color_black));
                tv_7.setTextColor(getResources().getColor(R.color.red));
                break;
        }

        selTyle = str_type[pos];
        Log.v("---sel stype ", selTyle);
        List<GoodsBean> temp = DataSupport.findAll(GoodsBean.class);
        list.clear();
        for (int i = 0; i < temp.size(); i++) {
            if (temp.get(i).getGoods_type().equals(selTyle) || selTyle.equals("全部")) {
                list.add(temp.get(i));
            }
        }
        Collections.reverse(list);
        adapter.notifyDataSetChanged();
    }

    private String selTyle;
    private String[] str_type = new String[]{"全部", "电子产品", "生活用品", "学习用品", "书籍", "衣物鞋类", "化妆品", "明星周边"};

    @Override
    public void onEvent(EventMessage msg) {
        super.onEvent(msg);
        if (msg.getMessageType() == EventMessage.REFRESH) {
            setSelPosit(position);
        }
    }
}
